package com.itheima.dao.store;

import com.itheima.domain.store.Catalog;
import com.itheima.domain.store.Company;
import com.itheima.domain.store.Question;
import com.itheima.domain.store.QuestionItem;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

/**
 * 题库管理  -> 题目选项管理(数据层)
 */
public interface QuestionItemDao {
    @Select("select * from st_question_item")
    @Results(id = "questionItemMap",value = {
            @Result(column = "id",property = "id",jdbcType = JdbcType.VARCHAR),
            @Result(column = "question_id",property = "questionId",jdbcType = JdbcType.VARCHAR),
            @Result(column = "content",property = "content",jdbcType = JdbcType.VARCHAR),
            @Result(column = "is_right",property = "isRight",jdbcType = JdbcType.VARCHAR)
    })
    List<QuestionItem> findAll() throws Exception;


    @Select("select * from st_question_item where id = #{id}")
    @ResultMap("questionItemMap")
    QuestionItem findById(String id) throws Exception;

    @Insert("INSERT INTO st_question_item (id, question_id, content,  is_right) VALUES (#{id,jdbcType=VARCHAR}, #{questionId,jdbcType=VARCHAR}, #{content,jdbcType=VARCHAR}, #{isRight,jdbcType=VARCHAR})")
    int save(QuestionItem questionItem)throws Exception;

    @Update("UPDATE st_question_item SET content = #{content,jdbcType=VARCHAR}, is_right = #{isRight,jdbcType=VARCHAR} WHERE id = #{id,jdbcType=VARCHAR}")
    int update(QuestionItem questionItem)throws Exception;

    @Delete("delete from st_question_item where id = #{id,jdbcType=VARCHAR}")
    int deleteById(String id)throws Exception;

    /**
     * 根据题干的ID，查询对应的所有的选项
     * @param questionId
     * @return
     * @throws Exception
     */
    @Select("SELECT * FROM st_question_item WHERE question_id = #{questionId}")
    @ResultMap("questionItemMap")
    List<QuestionItem> findByQuestionId(String questionId)throws Exception;

    @Delete("DELETE FROM st_question_item WHERE id IN ( SELECT * FROM( SELECT i.`id` FROM st_question_item i LEFT JOIN st_question q ON q.`id` = i.`question_id` WHERE q.id IS NULL )temp )")
    int clean();
}